package cn.edu.ctbu.test.api;

import cn.edu.ctbu.test.core.PageUtils;
import cn.edu.ctbu.test.domain.Score;
import cn.edu.ctbu.test.domain.Student;
import cn.edu.ctbu.test.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Slf4j
@RestController // RestController只能返回数据
@NoRepositoryBean
@RequestMapping("/api/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();

        return  students;
    }


    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypageStudent")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "condition",defaultValue = "") String condition)   {

        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.ASC,"id");

//        Pageable pageable= PageRequest.of(page,size,sort);
//        Page<Student> studentPage=studentService.findAll(pageable);
//
//        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
//
//        return pageUtils;

        Page<Student> studentPage;

        if(ObjectUtils.isEmpty(condition)){
            Pageable pageable = PageRequest.of(page,size,sort);
            studentPage= studentService.findAll(pageable);
        }else {
            Student student = new Student();
            student.setName(condition);

            Pageable pageable = PageRequest.of(0, 10, sort);

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("condition", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student> example = Example.of(student, matcher);

            studentPage = studentService.findAll(example, pageable);
        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());

        return pageUtils;


    }



    @GetMapping("/get/{id}")
    public Student get(@PathVariable Long id){

        Student student=studentService.getById(id);
        student.setPassWord("");

        return  student;//把id返回到客户端
    }


    /**
     * 以下为增删改查
     * @param
     * @return
     */
    @GetMapping("/findByName")
    public List<Student> findByName(String name){
        List<Student> students=studentService.findByName(name);

        return students;
    }


    @PostMapping("/insert") //添加人员不用返回人员，返回成功或者失败就可以了
    public Student insert(Student student){
        Student student1=studentService.insert(student);

        return student1;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){

        studentService.delete(id);
    }

    @PutMapping("/update")
    public Student update(Student student){

        //读取旧学生数据
        Student oldstudent=studentService.getById(student.getId());
        if(StringUtils.isEmpty(student.getPassWord())){
            //没有新密码，就是旧密码
            student.setPassWord(oldstudent.getPassWord());
        }

        student=studentService.update(student);

        return student;
    }



}
